package com.culturesalon.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.culturesalon.connectionfactory.ConnectionFactory;
import com.culturesalon.dto.Goods;
import com.culturesalon.dto.Orders;
import com.culturesalon.dto.User;
import com.culturesalon.util.OrderState;
import com.culturesalon.util.PaymentWay;

public class OrdersDAOImpl implements OrdersDAO {

	@Override
	public Serializable save(Orders orders) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		try {
			conn=ConnectionFactory.getConnection();
			StringBuffer sql=new StringBuffer();
			sql.append(" INSERT INTO T_ORDERS(ID,ORDERID,USERID,ORDERTIME,TOTALPRICE,ORDERSTATUS,");
			sql.append("LOGISTICSSTATE,DELIVERFEE,PAYABLEFEE,PAYSTATE,NOTE,PAYMENTWAY) ");
			sql.append(" VALUES(null,?,?,?,?,?,?,?,?,?,?,?) ");
			pstm=conn.prepareStatement(sql.toString(),Statement.RETURN_GENERATED_KEYS);
			pstm.setString(1,orders.getOrderId());
			pstm.setInt(2,orders.getUserId());
			pstm.setDate(3, new java.sql.Date(orders.getOrderTime().getTime()));
			pstm.setDouble(4, orders.getTotalPrice());
			pstm.setInt(5, orders.getOrderStatus().ordinal());
			pstm.setString(6, orders.getLogisticsState());
			pstm.setDouble(7, orders.getDeliverFee());
			pstm.setDouble(8, orders.getPayableFee());
			pstm.setBoolean(9, orders.getPayState());
			pstm.setString(10, orders.getNote());
			pstm.setInt(11, orders.getPaymentWay().ordinal());
			
			pstm.executeUpdate();
			rs=pstm.getGeneratedKeys();
			int id=0;
			if(rs.next()){
				id=rs.getInt(1);
				return id;
			}else{
				
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn,  rs,pstm);
		}
	}

	@Override
	public Serializable update(Orders orders) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstm = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE T_ORDERS SET orderId=?,");
		sql.append("userId=?,");
		sql.append("orderTime=?,totalPrice=?,orderStatus=?,logisticsState=?,");
		sql.append("deliverFee=?,payableFee=?,payState=?,note=?,paymentWay=?");
		sql.append(" WHERE ID=?");
		try {
			conn = ConnectionFactory.getConnection();
			pstm = conn.prepareStatement(sql.toString());
			pstm.setString(1,orders.getOrderId());
			pstm.setInt(2,orders.getUserId());
			pstm.setDate(3, new java.sql.Date(orders.getOrderTime().getTime()));
			pstm.setDouble(4, orders.getTotalPrice());
			pstm.setInt(5, orders.getOrderStatus().ordinal());
			pstm.setString(6, orders.getLogisticsState());
			pstm.setDouble(7, orders.getDeliverFee());
			pstm.setDouble(8, orders.getPayableFee());
			pstm.setBoolean(9, orders.getPayState());
			pstm.setString(10, orders.getNote());
			pstm.setInt(11, orders.getPaymentWay().ordinal());
			pstm.setInt(12, orders.getId());
			
			pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
						e.printStackTrace();
		}finally {
			ConnectionFactory.close(conn, null, pstm);
		}
		return sql;
	}

	@Override
	public void delete(Serializable id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteByOrderId(String orderId) {
		// TODO Auto-generated method stub

	}

	@Override
	public Orders get(Serializable id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ID,ORDERID,USERID,ORDERTIME,TOTALPRICE,ORDERSTATUS,");
		sql.append("LOGISTICSSTATE,DELIVERFEE,PAYABLEFEE,PAYSTATE,NOTE,PAYMENTWAY");
		sql.append(" FROM T_ORDERS ");
		sql.append(" WHERE ID=? ");
		conn = ConnectionFactory.getConnection();
		Orders orders = null;
		try {
			stmt = conn.prepareStatement(sql.toString());
			stmt.setInt(1, (Integer) id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				orders = new Orders();
				orders.setId(rs.getInt("id"));
				orders.setOrderId(rs.getString("orderId"));
				orders.setUserId(rs.getInt("userId"));
				orders.setOrderTime(rs.getDate("orderTime"));
				orders.setTotalPrice(rs.getDouble("totalPrice"));
				orders.setOrderStatus(OrderState.values()[rs.getInt("orderStatus")]);
				orders.setLogisticsState(rs.getString("logisticsState"));
				orders.setDeliverFee(rs.getDouble("deliverFee"));
				orders.setPayableFee(rs.getDouble("payableFee"));
				orders.setPayState(rs.getBoolean("payState"));
				orders.setNote(rs.getString("note"));
				orders.setPaymentWay(PaymentWay.values()[rs.getInt("paymentWay")]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, stmt);
		}
		return orders;
	}

	@Override
	public List<Orders> findAllByUser(User user) {
		// TODO Auto-generated method stub
		List<Orders> list = new ArrayList<Orders>();
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM T_ORDERS WHERE USERID=? ");
		conn = ConnectionFactory.getConnection();
		Orders orders = null;
		try {
			pstm = conn.prepareStatement(sql.toString());
			pstm.setInt(1, user.getId());
			rs = pstm.executeQuery();
			while (rs.next()) {
				orders = new Orders();
				orders.setId(rs.getInt("id"));
				orders.setOrderId(rs.getString("orderId"));
				orders.setUserId(rs.getInt("userId"));
				orders.setOrderTime(rs.getDate("orderTime"));
				orders.setTotalPrice(rs.getDouble("totalPrice"));
				orders.setOrderStatus(OrderState.values()[rs.getInt("orderStatus")]);
				orders.setLogisticsState(rs.getString("logisticsState"));
				orders.setDeliverFee(rs.getDouble("deliverFee"));
				orders.setPayableFee(rs.getDouble("payableFee"));
				orders.setPayState(rs.getBoolean("payState"));
				orders.setNote(rs.getString("note"));
				orders.setPaymentWay(PaymentWay.values()[rs.getInt("paymentWay")]);
				list.add(orders);

			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, pstm);

		}
		
		
	}

	@Override
	public List<Orders> findAll() {
		// TODO Auto-generated method stub
		List<Orders> list = new ArrayList<Orders>();
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM T_ORDERS ");
		conn = ConnectionFactory.getConnection();
		Orders orders = null;
		try {
			pstm = conn.prepareStatement(sql.toString());
			rs = pstm.executeQuery();
			while (rs.next()) {
				orders = new Orders();
				orders.setId(rs.getInt("id"));
				orders.setOrderId(rs.getString("orderId"));
				orders.setUserId(rs.getInt("userId"));
				orders.setOrderTime(rs.getDate("orderTime"));
				orders.setTotalPrice(rs.getDouble("totalPrice"));
				orders.setOrderStatus(OrderState.values()[rs.getInt("orderStatus")]);
				orders.setLogisticsState(rs.getString("logisticsState"));
				orders.setDeliverFee(rs.getDouble("deliverFee"));
				orders.setPayableFee(rs.getDouble("payableFee"));
				orders.setPayState(rs.getBoolean("payState"));
				orders.setNote(rs.getString("note"));
				orders.setPaymentWay(PaymentWay.values()[rs.getInt("paymentWay")]);
				list.add(orders);

			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, pstm);

		}
		
		
	}

	@Override
	public Orders getByOrderId(Serializable id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ID,ORDERID,USERID,ORDERTIME,TOTALPRICE,ORDERSTATUS,");
		sql.append("LOGISTICSSTATE,DELIVERFEE,PAYABLEFEE,PAYSTATE,NOTE,PAYMENTWAY");
		sql.append(" FROM T_ORDERS ");
		sql.append(" WHERE ORDERID=? ");
		conn = ConnectionFactory.getConnection();
		Orders orders = null;
		try {
			stmt = conn.prepareStatement(sql.toString());
			stmt.setString(1, (String) id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				orders = new Orders();
				orders.setId(rs.getInt("id"));
				orders.setOrderId(rs.getString("orderId"));
				orders.setUserId(rs.getInt("userId"));
				orders.setOrderTime(rs.getDate("orderTime"));
				orders.setTotalPrice(rs.getDouble("totalPrice"));
				orders.setOrderStatus(OrderState.values()[rs.getInt("orderStatus")]);
				orders.setLogisticsState(rs.getString("logisticsState"));
				orders.setDeliverFee(rs.getDouble("deliverFee"));
				orders.setPayableFee(rs.getDouble("payableFee"));
				orders.setPayState(rs.getBoolean("payState"));
				orders.setNote(rs.getString("note"));
				orders.setPaymentWay(PaymentWay.values()[rs.getInt("paymentWay")]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, stmt);
		}
		return orders;
	}



}
